import 'package:flame/components.dart';
import 'package:flutter_flame/routes/RouteGame.dart';
import 'package:flutter_flame/worlds/game/components/LawnCellPlantShadow.dart';
import '../../../../config/GameConfig.dart';
import 'plants/Plant.dart';

class LawnCell extends PositionComponent
    with HasGameReference<RouterGame>, GameConfig {
  final int row, col; // 行列
  Plant? plant; // 当前格子的植物
  LawnCellPlantShadow? plantShadow; // 种植虚影

  /// 草坪格子
  LawnCell(this.row, this.col);

  @override
  void onLoad() {
    priority = lawnCellPriority;
    size = cellSize;
    // 设置草坪格子的位置
    position = Vector2(
      col * cellSize.x + lawnTopLeft.x,
      row * cellSize.y + lawnTopLeft.y,
    );
    anchor = Anchor.topLeft;
    super.onLoad();
  }

  // 种植虚影
  void showPlantShadow() {
    if (plantShadow == null) {
      final plant = game.bloc.state.selectedPlant;
      plantShadow = LawnCellPlantShadow(plant!.image);
      add(plantShadow!);
      plantShadow!.position = plant.lawnCellShadowCellOffset!;
    }
  }

  void hidePlantShadow() {
    plantShadow?.removeFromParent();
    plantShadow = null;
  }

  // 移除植物
  void removePlant() {
    plant?.bodyShadow.removeFromParent();
    plant?.removeFromParent();
    plant = null;
  }
}
